//
//  ViewController.m
//  UI20-SAXAndDOM(XML)
//
//  Created by 韩旭 on 16/8/10.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "GDataXMLNode.h"
#import "XMLReader.h"
@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic, strong) NSMutableArray *stuArray;
@property (nonatomic, copy) NSString *elementName;
@property (nonatomic, strong) NSMutableArray *DomArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.DomArray = [NSMutableArray array];
}
// XML分为两种解析方式
//第一种是 逐行解析 -- 使用系统的解析器 NSXMLParser
- (IBAction)SAX解析:(id)sender {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSXMLParser *XML = [[NSXMLParser alloc] initWithData:data];
    XML.delegate = self;
    [XML parse];
}
// 1.解析器开始
- (void)parserDidStartDocument:(NSXMLParser *)parser {
    self.stuArray = [NSMutableArray array];
}
//解析器结束
- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSLog(@"%@", _stuArray);
}
// 2.节点头调用的方法, 记录当前节点, 方便后续赋值
//打印所有节点头
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {
     _elementName = elementName;
    if ([elementName isEqualToString:@"student"]) {
        Student *stu = [Student new];
        [_stuArray addObject:stu];
    }
}
// 节点的值
//根据节点值决定值, 没有值时对应的结果\n\t
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    //    NSLog(@"节点的值%@ -- 节点值结束", string);
//    if (![string hasPrefix:@" "] && ![string hasPrefix:@"\n"]) {
    if (_elementName) {
        Student *stu = [_stuArray lastObject];
        [stu setValue:string forKey:_elementName];
    }
    
//    }
}
// 4.遇到节点尾 调用的方法 方法的回调
// 判断当前节点结束,
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    _elementName = nil;
}
//解析失败时，调用
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSLog(@"解析失败时%@", parseError);
}

//第二种解析方式, DOM
//关于GDataXMLNode的引入
//1. 引入。h 与。m 文件
//2. 引入系统动态库libxml2.tbd
//3. 根据GDataXmlNode头文件中的提示添加/usr/include/libxml2
//4. 如果是ARC环境，需要对该文件进行MRC编译, 添加-fno-objc-arc
- (IBAction)DOM:(id)sender {
    NSString *string = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:string];
//   1. 创建GDataXmlDocument对象
//   2. 根据XML的Data数据生成一个树形结构的 Document
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
//    NSLog(@"%@", document);
//   2. 根据document获取根节点
    GDataXMLElement *rootElement = [document rootElement];
//   3. 根据根节点获取子节点数组
    NSArray *rootChild = [rootElement children];
//   4. 便利子节点数组
    for (GDataXMLElement *childElement in rootChild) {
//      5. 对应所有student界定啊
        Student *stu = [Student new];
        [_DomArray addObject:stu];
//        比那里student节点的所有子节点
            for (GDataXMLElement *grandSunElement in childElement.children) {
//                NSLog(@"%@", grandSunElement.name);
//                7. 赋值
                [stu setValue:grandSunElement.stringValue forKey:grandSunElement.name];
        }
    }
//    NSLog(@"%@", _DomArray);
//    XMLReader
//    将xml结果转化成NSDictionary
    NSError *error = nil;
    NSDictionary *dic = [XMLReader dictionaryForXMLData:data error:&error];
    NSLog(@"%@", dic);
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
